package com.yhf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yhf.pojo.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MyUserDetailService implements UserDetailsService {
    @Autowired
    UserService userService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List list = new ArrayList<>();
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);
        TbUser one = userService.getOne(queryWrapper);
        if (one.getUserName().equals("zhangsan")) {
            list = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_xiaoban");
        }
        if (one!=null){
            User user =  new User(one.getUserName(), one.getPassword(), list);
            return user;
        }
        return null;
    }
}
